<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;

class LoginController extends Controller
{
    public function index()
    {
    	return view('home.login');
    }
    //验证手机
    public function ajaxphone(Request $request)
    {
    	$phone=$request->phone;
    	$pattern='/\d{11}/';
    	$re=preg_match($pattern,$phone);
    	if(!$re)
    	{
    		return response()->json(5);
    	}
    	$res=DB::table('interviewee')->where('phone',$phone)->first();
    	if(!$res)
    	{
    		return response()->json(1);
    	}else
    	{
    		return response()->json(0);
    	}
  
    }
    //验证密码1
    public function ajaxpassword1(Request $request)
    {
    	$password1=$request->password1;
    	$pattern='/^[0-9a-zA-Z_]{6,18}$/';
    	$re=preg_match($pattern,$password1);
    	if(!$re)
    	{
    		return response()->json(0);
    	}else
    	{
    		return response()->json(1);
    	}
    	
    }
      //验证密码2
    public function ajaxpassword2(Request $request)
    {
    	$password2=$request->password2;
    	$password1=$request->password1;
    	
    	if(!$password1==$password2)
    	{
    		return response()->json(0);
    	}else
    	{
    		return response()->json(1);
    	}
    	
    }
    //手机注册
    public function register(Request $request)
    {
    	if($request->password1!=$request->password2)
    	{

    		return redirect('/home/login')->with(['inf'=>'两次密码不一致']);
    	}
    	if($request->a!='a' || $request->b!='b')
    	{
    		return redirect('/home/login');
    	}
    	$data=$request->except('_token','istest','a','b','c','regbutton','password2','password1','accept');
    	$data['password']=Crypt::encrypt($request->password1);
    	$data['token']=str_random(50);
    	$data['register_time']=time();
    	// dd($data);
    	
    	$res=DB::table('interviewee')->insert($data);
    	// dd($res);
    	if($res)
    	{
    		return redirect('/home/resume/first')->with(['is_login'=>1,'data'=>$data]);
    	}else
    	{
    		return redirect('/home/login')->with(['inf'=>'请重新登陆']);
    	}

    }
    // 注册协议
    public function regist()
    {
    	return view('home.regist');
    }
    //用户登录
    public function login(Request $request)
    {
    	$res=DB::table('interviewee')->where('phone',$request->loginname)->first();
    	if(!$res)
    	{
    		return response()->json(0);
    	}
    	if($request->password!=Crypt::decrypt($res->password))
    	{
    		return response()->json(2);
    	}else
    	{
    		return response()->json(9);
    	}

    

    }

    public function stop()
    {
        return view('home.stop');
    }


}
